package com.bootdo.system.controller;

import com.bootdo.system.config.MD5Utils;
import com.bootdo.system.domain.FrontuserDO;
import com.bootdo.system.domain.TibitableDO;
import com.bootdo.system.domain.ZichanhistoryDO;
import com.bootdo.system.service.FrontuserService;
import com.bootdo.system.service.TibitableService;
import com.bootdo.system.service.ZichanhistoryService;
import com.bootdo.system.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;

@ResponseBody
@Controller
public class TibiController extends BaseController {

    @Autowired
    private TibitableService tibitableService;
    @Autowired
    private ZichanhistoryService zichanhistoryService;
    @Autowired
    private FrontuserService frontuserService;


    @GetMapping("/huzhuan")
    public String huzhuan(HttpServletRequest request) {

        request.setAttribute("userdo", getUserDO(request));

        return "huzhuan";
    }


    @Transactional
    @GetMapping("/tibi/save")
    public R save(TibitableDO tibitableDO, HttpServletRequest request) {



        if (StringUtils.isEmpty(tibitableDO.getTibiaddress()) ||
                tibitableDO.getNum().compareTo(BigDecimal.ZERO) <= 0 ||
                StringUtils.isEmpty(tibitableDO.getPhonecode()) ||
                StringUtils.isEmpty(tibitableDO.getPaypass())) {

            return R.error("数据有空值，请检查");
        }
        //判断手续费不能小于零的错误
        tibitableDO.setShouxufei(new BigDecimal(3));
        //校验密码
        FrontuserDO userDO = getUserDO(request);
        String encrypt = MD5Utils.encrypt(userDO.getUsername(), tibitableDO.getPaypass());
        if (!encrypt.equals(userDO.getPaypass())) {
            return R.error("支付密码错误");
        }
        //短信
        final Object phone = request.getSession().getAttribute("phone");

        if (phone == null || !phone.toString().equals(tibitableDO.getPhonecode())) {

            return R.error("短信验证码错误");
        }
        request.getSession().removeAttribute("phone");

        //资产减少
        final BigDecimal yue = userDO.getZichan().subtract(tibitableDO.getNum()).subtract(tibitableDO.getShouxufei());
        if (yue.compareTo(BigDecimal.ZERO) < 0) {

            return R.error("金额超过账户余额");
        }
        //历史记录
        ZichanhistoryDO zichanhistoryDO = new ZichanhistoryDO();
        zichanhistoryDO.setCreatetime(new Date());
        BigDecimal add = tibitableDO.getNum().add(tibitableDO.getShouxufei());
        zichanhistoryDO.setAmount(BigDecimal.ZERO.subtract(add));
        zichanhistoryDO.setUserid(userDO.getUserid());
        zichanhistoryDO.setJiaoyitype("提现");
        zichanhistoryDO.setBeizhu("地址：" + tibitableDO.getTibiaddress());
        zichanhistoryService.save(zichanhistoryDO);
        //提币
        tibitableDO.setUserid(userDO.getUserid());
        tibitableDO.setUsername(userDO.getUsername());
        tibitableDO.setCreatetime(new Date());
        tibitableDO.setTibistatus(0);
        tibitableService.save(tibitableDO);
        System.out.println("提现后的余额=" + yue);
        //修改余额
        userDO.setZichan(yue);
        frontuserService.update(userDO);

        return R.ok("提币成功！");
    }


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @GetMapping("/tibi/huzhuan")
    public R huzhuan(TibitableDO tibitableDO, HttpServletRequest request) {

        if (
                tibitableDO.getNum().compareTo(BigDecimal.ZERO) <= 0 ||
                        StringUtils.isEmpty(tibitableDO.getPhonecode()) ||
                        StringUtils.isEmpty(tibitableDO.getUsername()) ||
                        StringUtils.isEmpty(tibitableDO.getPaypass())) {

            return R.error("数据有空值，请检查");
        }
        //校验密码
        FrontuserDO userDO = getUserDO(request);
        String encrypt = MD5Utils.encrypt(userDO.getUsername(), tibitableDO.getPaypass());
        if (!encrypt.equals(userDO.getPaypass())) {

            return R.error("支付密码错误");
        }
        //短信
        final Object phone = request.getSession().getAttribute("phone");
        if (phone == null || !phone.toString().equals(tibitableDO.getPhonecode())) {

            return R.error("短信验证码错误");
        }
        request.getSession().removeAttribute("phone");
        //判断手续费不能小于零的错误
        if (tibitableDO.getShouxufei().compareTo(BigDecimal.ZERO) < 0) {
            request.setAttribute("editpersoninfotip", "手续费低于0");

            return R.error("手续费低于0");
        }

        //资产减少
        BigDecimal yue = userDO.getZichan().subtract(tibitableDO.getNum()).subtract(tibitableDO.getShouxufei());
        if (yue.compareTo(BigDecimal.ZERO) < 0) {
            request.setAttribute("editpersoninfotip", "金额超过账户余额");

            return R.error("金额超过账户余额");
        }
        //检验用户存在
        FrontuserDO touser = frontuserService.getUserByUsername(tibitableDO.getUsername());
        if (touser == null) {
            request.setAttribute("editpersoninfotip", "接收人不存在");

            return R.error("接收人不存在");
        }
        //历史记录
        ZichanhistoryDO zichanhistoryDO = new ZichanhistoryDO();
        zichanhistoryDO.setCreatetime(new Date());
        BigDecimal add = tibitableDO.getNum().add(tibitableDO.getShouxufei());
        zichanhistoryDO.setAmount(BigDecimal.ZERO.subtract(add));
        zichanhistoryDO.setUserid(userDO.getUserid());
        zichanhistoryDO.setJiaoyitype("互转");
        zichanhistoryDO.setTouserid(touser.getUserid());
        zichanhistoryService.save(zichanhistoryDO);
        //提币
        tibitableDO.setUserid(userDO.getUserid());
        tibitableDO.setCreatetime(new Date());
        tibitableDO.setTibistatus(0);
        tibitableService.save(tibitableDO);
        //修改余额
        userDO.setZichan(yue);
        frontuserService.update(userDO);
        //接收人增加
        BigDecimal touseryue = touser.getZichan().add(tibitableDO.getNum());
        touser.setZichan(touseryue);
        frontuserService.update(touser);
        //接收人的历史
        ZichanhistoryDO zichanhistoryDO2 = new ZichanhistoryDO();
        zichanhistoryDO2.setCreatetime(new Date());
        BigDecimal add2 = tibitableDO.getNum().add(tibitableDO.getShouxufei());
        zichanhistoryDO2.setAmount(add2);
        zichanhistoryDO2.setUserid(touser.getUserid());
        zichanhistoryDO2.setJiaoyitype("互转");
        zichanhistoryService.save(zichanhistoryDO2);

        return R.ok("资金成功转出！");
    }
}
